package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class ArticleUpOrDownListener {

    @Autowired
    private IApArticleService apArticleService;

    @KafkaListener(topics = "${topic.upDownTopic}")
    public void upOrDown(ConsumerRecord<String,String> record){
        String mapStr = record.value();
            Map map = JSON.parseObject(mapStr, Map.class);

            Long articleId = (Long) map.get("articleId");
            Boolean enable = (Boolean) map.get("enable");

            ApArticle apArticle = apArticleService.getById(articleId);
            if(apArticle!=null){
            apArticle.setIsDown(!enable);
            apArticleService.updateById(apArticle);
        }
    }
}
